package com.BlogPark.controller;

import com.BlogPark.controller.exception.Code;
import com.BlogPark.controller.result.Result;
import com.BlogPark.mapper.CollectionsMapper;
import com.BlogPark.mapper.CommentsMapper;
import com.BlogPark.mapper.LikesMapper;
import com.BlogPark.pojo.Article;
import com.BlogPark.pojo.Comment;
import com.BlogPark.pojo.User;
import com.BlogPark.service.ShowArticleService.Impl.ShowArticleServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Auther: MFM
 * @Date: 2022/8/11 - 08 - 11 - 14:59
 * @version: 1.0
 * 展示文章详情响应
 */

@RestController
@RequestMapping("/Article")
public class ShowArticleController {
    @Autowired
    private ShowArticleServiceImpl showArticleService;

    //添加文章获取likes、comment、collections（业峰）
    @Autowired
    private LikesMapper likesMapper;
    @Autowired
    private CommentsMapper commentsMapper;
    @Autowired
    private CollectionsMapper collectionsMapper;

    //方法整合controller
    @GetMapping("/{articleId}")
    public Result ShowArticle(@PathVariable String articleId) throws Exception {
        Article article = showArticleService.FindArticle(articleId);
        if (article == null) {
            return new Result(Code.GET_ERR, null, "未找到该文章，文章已被删除");
        }
        //添加文章获取likes、comment、collections（业峰）
        article.setLikes(likesMapper.getLikes(article.getId()));
        article.setComments(commentsMapper.getComments(article.getId()));
        article.setCollection(collectionsMapper.getCollections(article.getId()));

        return new Result(Code.GET_OK, article);
    }

    //展示评论
    @GetMapping("/ShowComments/{articleId}")
    public Result ShowComments(@PathVariable String articleId) throws Exception {
        Article article = showArticleService.FindArticle(articleId);
        if (article == null) {
            return new Result(Code.GET_ERR, null, "未找到该文章，文章已被删除");
        }
        Map<String, List<Comment>> comments = showArticleService.ShowComments(articleId);


        return new Result(Code.GET_OK, comments);
    }

    //点赞功能
    @PostMapping("/Likes")
    public Result Likes(@RequestParam("userId") Integer userId, @RequestParam("articleId") String articleId) throws Exception {
        if (articleId == null || "".equals(articleId) || userId == -1) {
            return new Result(Code.SAVE_ERR, null, "点赞失败 请进行登录后重新尝试");
        }
        User user = new User();
        user.setId(userId);

        showArticleService.Likes(user, articleId);
        return new Result(Code.SAVE_OK, null);
    }

    //是否已经点赞
    @PostMapping("/isLikes")
    public Result isLikes(@RequestParam("userId") Integer userId, @RequestParam("headTo") String headTo) throws Exception {
        if (userId == -1) {
            return new Result(Code.GET_ERR, "{'isLike':'false'}", "用户不存在，请登录后重试");
        }

        User user = new User();
        user.setId(userId);

        boolean likes = showArticleService.isLikes(user, headTo);

        return new Result(Code.GET_OK, "{\"isLike\":" + likes + "}", "");
    }

    //评论功能
    @PostMapping("/AddComments")
    public Result AddComments(@RequestParam("userId") Integer userId, @RequestParam("articleId") String articleId, @RequestParam("comment") String comment) throws Exception {
        User user = new User();
        user.setId(userId);
        boolean comments = showArticleService.AddComments(user, articleId, comment);
        if (!comments) {
            return new Result(Code.SAVE_ERR, null, "评论失败，请刷新网页后再试");
        }
        return new Result(Code.SAVE_OK, true);
    }

    @PostMapping("/DeleteComments")
    public Result DeleteComments(@RequestParam("userId") Integer userId, @RequestParam("commentId") String commentId) throws Exception {
        if (userId == -1) {
            return new Result(Code.SAVE_ERR, null, "用户未登录，请刷新网页后再试");
        }

        User user = new User();
        user.setId(userId);
        boolean comments = showArticleService.DeleteComments(user, commentId);
        if (!comments) {
            return new Result(Code.SAVE_ERR, null, "评论失败，请刷新网页后再试");
        }
        return new Result(Code.DELETE_OK, true);
    }

    //收藏功能
    @PutMapping("/AddCollection/{userId}&{articleId}")
    public Result AddCollection(@PathVariable Integer userId, @PathVariable String articleId) throws Exception {
        User user = new User();
        user.setId(userId);
        boolean collection = showArticleService.AddCollection(user, articleId);
        if (!collection) {
            return new Result(Code.SAVE_ERR, null, "收藏失败，请刷新网页后再试");
        }
        return new Result(Code.SAVE_OK, true);
    }

    @DeleteMapping("/DeleteCollection/{userId}&{articleId}")
    public Result DeleteCollection(@PathVariable("userId") Integer userId, @PathVariable("articleId") String articleId) throws Exception {
        User user = new User();
        user.setId(userId);
        boolean collection = showArticleService.DeleteCollection(user, articleId);
        if (!collection) {
            return new Result(Code.SAVE_ERR, null, "取消收藏失败，请刷新网页后再试");
        }
        return new Result(Code.DELETE_OK, true);
    }

    //是否收藏
    @PostMapping("isCollection")
    public Result isCollection(@RequestParam Integer userId, @RequestParam String articleId) throws Exception {

        User user = new User();
        user.setId(userId);
        boolean collection = showArticleService.isCollection(user, articleId);
        if (!collection) {
            return new Result(Code.SAVE_ERR, false, "取消收藏失败，请刷新网页后再试");
        }

        return new Result(Code.GET_OK, true);
    }


    //-----------------------------------------------------------------------单独方法

    //展示标题
    @GetMapping("/ShowTitle/{articleId}")
    public Result ShowTitle(@PathVariable String articleId) throws Exception {
        String title = showArticleService.ShowTitle(articleId);
        title = title + "";
        if ("".equals(title)) {
            return new Result(Code.GET_ERR, title, "未找到该文章，文章已被删除");
        }
        return new Result(Code.GET_OK, title);
    }

    //展示分类和标签
    @GetMapping("/ShowLabels/{articleId}")
    public Result ShowLabels(@PathVariable String articleId) throws Exception {
        String title = showArticleService.ShowLabels(articleId);
        if (title == null) {
            return new Result(Code.GET_ERR, title, "未找到该文章，文章已被删除");
        }
        return new Result(Code.GET_OK, title);
    }

    //展示浏览量 收藏量  创作时间
    @GetMapping("/ShowViews/{articleId}")
    public Result ShowViews(@PathVariable String articleId) throws Exception {
        int i = showArticleService.ShowViews(articleId);
        if (i == -1) {
            return new Result(Code.GET_ERR, null, "未找到该文章，文章已被删除");
        }
        return new Result(Code.GET_OK, i);
    }

    @GetMapping("/ShowCollections/{articleId}")
    public Result ShowCollections(@PathVariable String articleId) throws Exception {
        int i = showArticleService.ShowCollections(articleId);
        if (i == -1) {
            return new Result(Code.GET_ERR, null, "未找到该文章，文章已被删除");
        }
        return new Result(Code.GET_OK, i);
    }

    @GetMapping("/ShowDate/{articleId}")
    public Result ShowDate(@PathVariable String articleId) throws Exception {
        Date date = showArticleService.ShowDate(articleId);
        if (date == null) {
            return new Result(Code.GET_ERR, null, "未找到该文章，文章已被删除");
        }
        return new Result(Code.GET_OK, date);
    }

    //展示内容
    @GetMapping("/ShowContent/{articleId}")
    public Result ShowContent(@PathVariable String articleId) throws Exception {
        String s = showArticleService.ShowContent(articleId);
        s += "";
        if ("".equals(s)) {
            return new Result(Code.GET_ERR, null, "未找到该文章，文章已被删除");
        }
        return new Result(Code.GET_OK, s);
    }

    //展示作者信息 粉丝数 浏览量 点赞量 评论量 收藏量
    @GetMapping("/ShowNickname/{userId}")
    public Result ShowNickname(@PathVariable int userId) throws Exception {
        String s = showArticleService.ShowNickname(userId);
        s += "";
        if ("".equals(s)) {
            return new Result(Code.GET_ERR, null, "查无此人");
        }
        return new Result(Code.GET_OK, s);
    }

    @GetMapping("/ShowUserFans/{userId}")
    public Result ShowUserFans(@PathVariable int userId) throws Exception {
        int i = showArticleService.ShowUserFans(userId);
        if (i == -1) {
            return new Result(Code.GET_ERR, null, "查无此人");
        }
        return new Result(Code.GET_OK, i);
    }

    @GetMapping("/ShowUserViews/{userId}")
    public Result ShowUserViews(@PathVariable Integer userId) throws Exception {
        int i = showArticleService.ShowUserViews(userId);
        if (i == -1) {
            return new Result(Code.GET_ERR, null, "查无此人");
        }
        return new Result(Code.GET_OK, i);
    }

    @GetMapping("/ShowUserLikes/{userId}")
    public Result ShowUserLikes(@PathVariable Integer userId) throws Exception {
        int i = showArticleService.ShowUserLikes(userId);
        if (i == -1) {
            return new Result(Code.GET_ERR, null, "查无此人");
        }
        return new Result(Code.GET_OK, i);
    }

    @GetMapping("/ShowUserComments/{userId}")
    public Result ShowUserComments(@PathVariable Integer userId) throws Exception {
        int i = showArticleService.ShowUserComments(userId);
        if (i == -1) {
            return new Result(Code.GET_ERR, null, "查无此人");
        }
        return new Result(Code.GET_OK, i);
    }

    @GetMapping("/ShowUserCollections/{userId}")
    public Result ShowUserCollections(@PathVariable Integer userId) throws Exception {
        int i = showArticleService.ShowUserCollections(userId);
        if (i == -1) {
            return new Result(Code.GET_ERR, null, "查无此人");
        }
        return new Result(Code.GET_OK, i);
    }




    //查看他人文章有哪些
    @GetMapping("/ShowUserArticle/{userId}")
    public Result ShowUserArticle(@PathVariable Integer userId) throws Exception {
        List<Article> articles = showArticleService.ShowUserArticle(userId);
        if (articles.isEmpty()) {
            return new Result(Code.GET_ERR, null, "未找到该用户文章");
        }

        return new Result(Code.GET_OK, articles);
    }

}
